package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.zhgd.util.PageUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.zhgd.domain.ZjHntScale;
import com.ruoyi.zhgd.dto.NSysStationDto;
import com.ruoyi.zhgd.dto.ZjHntScaleDto;
import com.ruoyi.zhgd.mapper.NSysStationMapper;
import com.ruoyi.zhgd.mapper.ZjHntScaleMapper;
import com.ruoyi.zhgd.mapper.ZjHntScaleRefMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.ZjHntScaleRefBo;
import com.ruoyi.zhgd.domain.vo.ZjHntScaleRefVo;
import com.ruoyi.zhgd.domain.ZjHntScaleRef;
import com.ruoyi.zhgd.service.IZjHntScaleRefService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 配比   自建-混凝土-配比Service业务层处理
 *
 * @author yhy
 * @date 2023-09-22
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class ZjHntScaleRefServiceImpl implements IZjHntScaleRefService {

    private final ZjHntScaleRefMapper baseMapper;

    @Autowired
    private ZjHntScaleMapper zjHntScaleMapper;

    @Autowired
    private NSysStationMapper stationMapper;



    /**
     * 查询配比   自建-混凝土-配比
     */
    @Override
    public ZjHntScaleDto queryById(Long id){
        ZjHntScaleDto zjHntScaleDto = new ZjHntScaleDto();
        ZjHntScale zjHntScale = zjHntScaleMapper.selectId(id);
        BeanUtils.copyProperties(zjHntScale,zjHntScaleDto);
        List<ZjHntScaleRef> zjHntScaleRefs = baseMapper.selectByList(id);
        zjHntScaleDto.setZjHntScaleRefList(zjHntScaleRefs);
        return zjHntScaleDto;
    }

    /**
     * 查询配比   自建-混凝土-配比列表
     */
    @Override
    public TableDataInfo<ZjHntScaleRefVo> queryPageList(ZjHntScaleRefBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZjHntScaleRef> lqw = buildQueryWrapper(bo);
        Page<ZjHntScaleRefVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询配比   自建-混凝土-配比列表
     */
    @Override
    public List<ZjHntScaleRefVo> queryList(ZjHntScaleRefBo bo) {
        LambdaQueryWrapper<ZjHntScaleRef> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZjHntScaleRef> buildQueryWrapper(ZjHntScaleRefBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZjHntScaleRef> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMatchingId() != null, ZjHntScaleRef::getMatchingId, bo.getMatchingId());
        lqw.eq(bo.getMaterialDosage() != null, ZjHntScaleRef::getMaterialDosage, bo.getMaterialDosage());
        lqw.eq(bo.getMaterialMatching() != null, ZjHntScaleRef::getMaterialMatching, bo.getMaterialMatching());
        lqw.eq(bo.getUpperLimit() != null, ZjHntScaleRef::getUpperLimit, bo.getUpperLimit());
        lqw.eq(bo.getLowerLimit() != null, ZjHntScaleRef::getLowerLimit, bo.getLowerLimit());
        return lqw;
    }

    /**
     * 新增配比   自建-混凝土-配比
     */
    @Override
    public Boolean insertByBo(ZjHntScaleRef bo) {
        ZjHntScaleRef add = BeanUtil.toBean(bo, ZjHntScaleRef.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改配比   自建-混凝土-配比
     */
    @Override
    public Boolean updateByBo(ZjHntScaleRef bo) {
        ZjHntScaleRef update = BeanUtil.toBean(bo, ZjHntScaleRef.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZjHntScaleRef entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除配比   自建-混凝土-配比
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 混凝土列表
     * @param page
     * @param params
     * @return
     */
    @Override
    public PageUtils hntScaleList(Page page, Map<String, Object> params) {
        List<Map> hntScaleList = baseMapper.hntScaleList(page, params);
        return new PageUtils(page.setRecords(hntScaleList));
    }

    /**
     * 新增
     * @param zjHntScaleDto
     * @return
     */
    @Override
    public void add(ZjHntScaleDto zjHntScaleDto) {
        ZjHntScale zjHntScale = new ZjHntScale();
        BeanUtils.copyProperties(zjHntScaleDto,zjHntScale);
        List<NSysStationDto> station = stationMapper.station(zjHntScaleDto.getIdentifier());
        for (NSysStationDto nSysStationDto : station) {
            zjHntScale.setIdentifier(nSysStationDto.getIdentifier());
        }
        zjHntScaleMapper.insert(zjHntScale);
        insertOrUpdarte(zjHntScaleDto,zjHntScale.getId());
    }

    /**
     * 删除
     * @param id
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void removeRequires(String id) {
        QueryWrapper<ZjHntScaleRef> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("matching_id",id);
        baseMapper.delete(queryWrapper);
        zjHntScaleMapper.deleteById(id);
    }

    /**
     * 修改
     * @param zjHntScaleDto
     * @return
     */
    @Override
    public void edit(ZjHntScaleDto zjHntScaleDto) {
        ZjHntScale zjHntScale = new ZjHntScale();
        zjHntScaleMapper.deleteById(zjHntScaleDto.getId());
        delete(zjHntScaleDto.getId());
        BeanUtils.copyProperties(zjHntScaleDto,zjHntScale);
        zjHntScaleMapper.insert(zjHntScale);
        insertOrUpdarte(zjHntScaleDto,zjHntScale.getId());

    }

    public void insertOrUpdarte(ZjHntScaleDto zjHntScaleDto,String id){
        if (!CollectionUtils.isEmpty(zjHntScaleDto.getZjHntScaleRefList())){
            List<ZjHntScaleRef> list =  zjHntScaleDto.getZjHntScaleRefList().stream().map(ref -> {
                ZjHntScaleRef zjHntScaleRef = new ZjHntScaleRef();
                zjHntScaleRef.setMaterialDosage(ref.getMaterialDosage());
                zjHntScaleRef.setLowerLimit(ref.getLowerLimit());
                zjHntScaleRef.setUpperLimit(ref.getUpperLimit());
                zjHntScaleRef.setMaterialMatching(ref.getMaterialMatching());
                zjHntScaleRef.setMaterialStoreName(ref.getMaterialStoreName());
                zjHntScaleRef.setMaterialStoreType(ref.getMaterialStoreType());
                zjHntScaleRef.setStationId(ref.getStationId());
                zjHntScaleRef.setMatchingId(id);
                return zjHntScaleRef;
            }).collect(Collectors.toList());
            baseMapper.insertBatch(list);
        }
    }


    public void  delete(String id){
        List<ZjHntScaleRef> zjHntScaleRefs = baseMapper.selectByList(Long.valueOf(id));
        for (ZjHntScaleRef zjHntScaleRef : zjHntScaleRefs) {
            baseMapper.updateByDeleteId(zjHntScaleRef.getId());
        }
    }
}
